// myself/pages/energy-detail/energyDetail.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total:0,
    params:{
      pageSize:15,
      currentPage:1
    },
    cdnPath:app.globalData.CDNPATH,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getEnergyRecord()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const { total, list, params } = this.data
    const { currentPage } = params
    if (Number(total) > list.length) {
      this.setData({
        'params.currentPage': currentPage + 1
      })
      this.getEnergyRecord()
    }
  },
  getEnergyRecord () {
    const { currentPage } = this.data.params 
    app.api.post({
      url: 'get_energy_record',
      data: this.data.params
    }).then(res => {
      let { list } = this.data
      currentPage === 1 ? (list = res.list) : list.push(...res.list)
      this.setData({ list, total: res.total })
    })
  },
})